//
//  LMBaseViewController.m
//  LMDuanZi
//
//  Created by 刘明 on 17/3/20.
//  Copyright © 2017年 刘明. All rights reserved.
//

#import "LMBaseViewController.h"

#import "AppDelegate.h"


@interface LMBaseViewController ()


@end

@implementation LMBaseViewController


#pragma mark - View & LifeCycle

- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    [UIView setAnimationsEnabled:YES];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    

}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
}


- (void)viewDidLoad {
    [super viewDidLoad];

    [UIView setAnimationsEnabled:YES];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.edgesForExtendedLayout = UIRectEdgeAll;
    self.view.backgroundColor = LMWhiteColor;

    
    Reachability *reach = [Reachability reachabilityWithHostName:@"www.baidu.com"];
    self.reach = reach;
    [[NSNotificationCenter defaultCenter]addObserver:self  selector:@selector(netStatusChange:) name:kReachabilityChangedNotification object:nil];
    //实现监听
    [reach startNotifier];

}



#pragma mark - Action

#pragma mark - Private

-(void)dealloc
{
    //移除通知
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}

//通知监听回调 网络状态发送改变 系统会发出一个kReachabilityChangedNotification通知，然后会触发此回调方法
- (void)netStatusChange:(NSNotification *)noti{
    //    NSLog(@"-----%@",noti.userInfo);
    //判断网络状态
    switch (self.reach.currentReachabilityStatus) {
        case NotReachable:{
            UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"当前网络连接失败，请查看设置" preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
            }];
            
            UIAlertAction *otherAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                
                NSURL * url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
                if ([[UIApplication sharedApplication]canOpenURL:url]) {
                    [[UIApplication sharedApplication]openURL:url];
                }
                
            }];
            
            // Add the actions.
            [alertController addAction:cancelAction];
            [alertController addAction:otherAction];
            [self presentViewController:alertController animated:YES completion:nil];
        }
            break;
        case ReachableViaWiFi:
            //            NSLog(@"wifi上网2");
            break;
        case ReachableViaWWAN:
            //            NSLog(@"手机上网2");
            break;
        default:
            break;
    }
}


#pragma mark - setter

#pragma mark - getter

#pragma mark - lazy



#pragma mark - MemoryWarning

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
